package com.unb.mobilehandover.visao;

import java.util.List;

import com.example.myfirstapplication.R;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;
// import com.unb.mobilehandover.redes.*;

public class WifiConnections extends Activity {
    
    // atributos para wifi
	TextView mainText;
    WifiManager mainWifi;
    WifiReceiver receiverWifi;
    StringBuilder sb = new StringBuilder();
    List<ScanResult> wifiList;
    
    // atributos para 3g
    SignalStrengthListener signalStrengthListener;
    TextView signalStrengthTextView;
    
     
    public void onCreate(Bundle savedInstanceState) {
         
       super.onCreate(savedInstanceState);
        
       
       ////////////////// PARTE DO RELATIVO A SCAN DE REDES WIFI
       setContentView(R.layout.activity_wifi_and_3g_connections);
       mainText = (TextView) findViewById(R.id.mainText);
        
       // Initiate wifi service manager
       mainWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        
       // Check for wifi is disabled
       if (mainWifi.isWifiEnabled() == false)
            {   
                // If wifi disabled then enable it
                Toast.makeText(getApplicationContext(), "Wifi desabilitada..ativando", 
                Toast.LENGTH_LONG).show();
                 
                mainWifi.setWifiEnabled(true);
            } 
        
       // wifi scaned value broadcast receiver 
       receiverWifi = new WifiReceiver();
                
       // Register broadcast receiver 
       // Broacast receiver will automatically call when number of wifi connections changed
       registerReceiver(receiverWifi, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
       mainWifi.startScan();
       mainText.setText("\n\n\n\n\n______________________________\n" +
                                  "                WI-FI              \n" +
    		                      "______________________________\n" +
                                  "Iniciando scan...");
       
       ///////////////// SCAN DA REDE 3G
     //setup content stuff
       //this.setContentView(R.layout.activity_signal_strength);
       signalStrengthTextView = (TextView)findViewById(R.id.signalStrengthTextView);

       //start the signal strength listener
      signalStrengthListener = new SignalStrengthListener();	           
       ((TelephonyManager)getSystemService(TELEPHONY_SERVICE)).listen(signalStrengthListener,SignalStrengthListener.LISTEN_SIGNAL_STRENGTHS);
     }
    
 
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(0, 0, 0, "Refresh");
        return super.onCreateOptionsMenu(menu);
    }
 
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        mainWifi.startScan();
        mainText.setText("\n\n\n\n\n______________________________\n" +
                                   "                WI-FI              \n" +
                                   "______________________________\n" +
                                   "Iniciando scan...");
        return super.onMenuItemSelected(featureId, item);
    }

    protected void onPause() {
        unregisterReceiver(receiverWifi);
        super.onPause();
    }
 
    protected void onResume() {
        registerReceiver(receiverWifi, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
        super.onResume();
    }
    
    class WifiReceiver extends BroadcastReceiver {
        public void onReceive(Context c, Intent intent) {
            sb = new StringBuilder();
            wifiList = mainWifi.getScanResults();
            sb.append("\n\n\n\n\n______________________________\n" +
                                "                WI-FI              \n" +
	                            "______________________________\n" +
                                "Redes identificadas: "+wifiList.size()+"\n\n");
            
            for(int i = 0; i < wifiList.size(); i++){
                sb.append(new Integer(i+1).toString() + ".");
                sb.append(wifiList.get(i).SSID + ": " + wifiList.get(i).level);
                sb.append("\n");
            }
            mainText.setText(sb);
        }
    }
    
    private class SignalStrengthListener extends PhoneStateListener
    {
     @Override
     public void onSignalStrengthsChanged(android.telephony.SignalStrength signalStrength) {
       
        // get the signal strength (a value between 0 and 31)
        int strengthAmplitude = signalStrength.getGsmSignalStrength();
        int strengthAmplitudeAsDBM = strengthAmplitude * 2 - 113;
       
   	    //do something with it (in this case we update a text view)
   	    signalStrengthTextView.setText("______________________________\n" +
                                       "                 3G                \n" +
    		                           "______________________________\n" +
                                       "Nivel de sinal GSM: " + String.valueOf(strengthAmplitudeAsDBM));		
   	    super.onSignalStrengthsChanged(signalStrength);
   	  }
      }
     
    
}